package com.ruoyi.mechanismType.service.impl;

import java.util.List;
import java.util.stream.Collectors;

import com.ruoyi.mechanismType.dto.MechanismTypeDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.mechanismType.mapper.MechanismTypeMapper;
import com.ruoyi.mechanismType.domain.MechanismType;
import com.ruoyi.mechanismType.service.IMechanismTypeService;

/**
 * 设备类型Service业务层处理
 *
 * @author pengjie
 * @date 2023-12-21
 */
@Service
public class MechanismTypeServiceImpl implements IMechanismTypeService
{
    @Autowired
    private MechanismTypeMapper mechanismTypeMapper;

    /**
     * 查询设备类型
     *
     * @param id 设备类型主键
     * @return 设备类型
     */
    @Override
    public MechanismType selectMechanismTypeById(String id)
    {
        return mechanismTypeMapper.selectMechanismTypeById(id);
    }

    /**
     * 查询设备类型列表
     *
     * @param mechanismType 设备类型
     * @return 设备类型
     */
    @Override
    public List<MechanismType> selectMechanismTypeList(MechanismType mechanismType)
    {
        return mechanismTypeMapper.selectMechanismTypeList(mechanismType);
    }

    /**
     * 新增设备类型
     *
     * @param mechanismType 设备类型
     * @return 结果
     */
    @Override
    public int insertMechanismType(MechanismType mechanismType)
    {
        return mechanismTypeMapper.insertMechanismType(mechanismType);
    }

    /**
     * 修改设备类型
     *
     * @param mechanismType 设备类型
     * @return 结果
     */
    @Override
    public int updateMechanismType(MechanismType mechanismType)
    {
        return mechanismTypeMapper.updateMechanismType(mechanismType);
    }

    /**
     * 批量删除设备类型
     *
     * @param ids 需要删除的设备类型主键
     * @return 结果
     */
    @Override
    public int deleteMechanismTypeByIds(String[] ids)
    {
        return mechanismTypeMapper.deleteMechanismTypeByIds(ids);
    }

    /**
     * 删除设备类型信息
     *
     * @param id 设备类型主键
     * @return 结果
     */
    @Override
    public int deleteMechanismTypeById(String id)
    {
        return mechanismTypeMapper.deleteMechanismTypeById(id);
    }


    /**
     * 查询设备类型列表树
     */
    @Override
    public List<MechanismType> selectMechanismTypequeryTreeData() {
        MechanismType mechanismType=new MechanismType();
        List<MechanismType> mechanismTypeList=mechanismTypeMapper.selectMechanismTypeList(mechanismType);
        List<MechanismType> list =mechanismTypeList.stream().filter(at->"0".equals(at.getParentId())).map(
                it->{
                    it.setChildren(getChildren(it,mechanismTypeList));
                    return  it;
                }
        ).collect(Collectors.toList());
        return list ;
    }

    /**
     * 获得列表树的子树
     */
    public List<MechanismType> getChildren(MechanismType mechanismType, List<MechanismType> mechanismTypeList) {
        List<MechanismType> list=mechanismTypeList.stream().filter(at->mechanismType.getId().equals(at.getParentId())).map(
                it->{
                    it.setChildren(getChildren(it,mechanismTypeList));
                    return it;
                }
        ).collect(Collectors.toList());
        return list;
    }



    /**
     * 封装MechanismTypeDto
     */
    @Override
    public List<MechanismTypeDto> getDto(List<MechanismType> list) {
        List<MechanismTypeDto> mechanismTypeDtoList=list.stream().map(it->{
            MechanismTypeDto mechanismTypeDto =new MechanismTypeDto();
            mechanismTypeDto.setKey(it.getId());
            mechanismTypeDto.setValue(it.getId());
            mechanismTypeDto.setLabel(it.getDeviceType());
            mechanismTypeDto.setTitle(it.getDeviceType());
            mechanismTypeDto.setLeaf(true);
            if (!it.getChildren().isEmpty()){
                mechanismTypeDto.setChildren(getDto(it.getChildren()));
            }else {
                mechanismTypeDto.setChildren(null);
            }
            return mechanismTypeDto;
        }).collect(Collectors.toList());
        return mechanismTypeDtoList;
    }
}
